//
//  HYWarningRecordVC.m
//  TJHYHardwareProduct
//
//  Created by yc on 2017/12/25.
//  Copyright © 2017年 Cong Yao. All rights reserved.
//

#import "HYWarningRecordVC.h"
#import "HYWarningRecordView.h"
#import "HYWarningRecordTableViewCell.h"
#import "HYWarningListApi.h"
#import "DTPickerManager.h"
#import "HYDateButtonView.h"

@interface HYWarningRecordVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)HYWarningRecordView *containerView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,assign)int page;
@property(nonatomic,copy)NSString *time;

@property(nonatomic,strong)UILabel *emptyLabel;


@end

@implementation HYWarningRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = 1;
    self.time = [self getNowDateString];
    [self updateListWithPage:self.page time:self.time];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)updateListWithPage:(int)page time:(NSString *)timeString
{
    [HYWarningListApi getWarningList:page dno:0 ctime:timeString success:^(NSArray<HYWarnInfoModel *> *list) {

        [self.containerView.tableView.mj_footer endRefreshing];
        [self.containerView.tableView.mj_header endRefreshing];
        if (self.page == 1)
        {
            if (list.count == 0)
            {
                self.emptyLabel.hidden = NO;
            }
            else
            {
                self.emptyLabel.hidden = YES;
            }
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:list];
            [self.containerView.tableView reloadData];
        }
        else
        {
            if (list.count != 0)
            {
                [self.dataSource addObjectsFromArray:list];
                [self.containerView.tableView reloadData];
            }
            else
            {
                [self.containerView.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    } fail:^(NSDictionary *dic) {
        [self.containerView.tableView.mj_footer endRefreshing];
        [self.containerView.tableView.mj_header endRefreshing];
    }];
}

- (NSString *)getNowDateString
{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:date];
}


#pragma mark --- 点击事件
- (void)didClickDateView
{
    __weak typeof(self) weakSelf = self;
    [[DTPickerManager shareManager] showDate:[NSDate date] pickerWithBirthDayAndPicker:^(NSArray *arr) {
        
    } andConfirm:^(NSArray *arr) {
        if (arr.count == 3)
        {
            NSString *time = [NSString stringWithFormat:@"%@-%@-%@",arr[0],arr[1],arr[2]];
            weakSelf.containerView.startView.dateLabel.text = time;
            weakSelf.time = time;
            weakSelf.page = 1;
            [weakSelf updateListWithPage:weakSelf.page time:time];
        }
    }];
}

#pragma mark - TableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(600);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYWarningRecordTableViewCell *cell = [HYWarningRecordTableViewCell cellWithTableView:tableView forIndexPath:indexPath];
    [cell updateDataWithWarnInfoModel:self.dataSource[indexPath.section]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - 懒加载
- (HYWarningRecordView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[HYWarningRecordView alloc]init];
        [superView addSubview:_containerView];
        _containerView.tableView.delegate = self;
        _containerView.tableView.dataSource = self;
        _containerView.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self updateListWithPage:self.page time:self.time];
        }];
        _containerView.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page ++;
            [self updateListWithPage:self.page time:self.time];
        }];
        _containerView.startView.dateLabel.text = [self getNowDateString];
        UITapGestureRecognizer *tan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickDateView)];
        [_containerView.startView addGestureRecognizer:tan];
    }
    return _containerView;
}

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (UILabel *)emptyLabel
{
    if(!_emptyLabel)
    {
        UIView *superView = self.view;
        _emptyLabel = [[UILabel alloc]init];
        [superView addSubview:_emptyLabel];
        _emptyLabel.font = fontWithSize(15);
        _emptyLabel.textColor = [UIColor darkGrayColor];
        _emptyLabel.text = @"暂无满足条件的数据";
    }
    return _emptyLabel;
}



#pragma mark - 布局
- (void)base_masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.right.bottom.mas_equalTo(superView);
    }];
    
    [self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(superView);
    }];
}

#pragma mark - 导航栏
- (void)base_configNavigationBar
{
    [self setNavigationTitle:@"报警记录" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor whiteColor];
}



@end
